/*
 * 作者:熊猫
 * 日期:2017 - 3 - 15 0 - 2 - 57
 *
 */

package net.lainiao.morethread.Chapter6.demo4;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/15.
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        DelayQueue<Event> queue = new DelayQueue<>();
        Thread[] threads = new Thread[5];
        for (int i = 0; i < threads.length; i++) {
            Task task = new Task(i + 1, queue);
            threads[i] = new Thread(task);
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].start();
        }
        for (int i = 0; i < threads.length; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        do {
            int count = 0;
            Event event;
            do {
                event = queue.poll();
                if (event != null) {
                    count++;
                }
            } while (event != null);
            if(count>0){
                System.out.println("在时间为:"+format.format(new Date())+"时刻数量为:"+count);
            }
            TimeUnit.MILLISECONDS.sleep(500);
        } while (queue.size() > 0);
        System.out.println("Main Over");
    }
}
